import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserTableInfoExtractor {

    public static void main(String[] args) {
        // 数据库连接信息
        String url = "jdbc:mysql://localhost:3306/zdyn";
        String user = "root";
        String password = "root@123456";

        // 输出文件路径
        String outputFilePath = "d:/user_table_info.txt";

        try (Connection connection = DriverManager.getConnection(url, user, password);
             FileWriter fileWriter = new FileWriter(outputFilePath)) {

            // 获取数据库元数据
            DatabaseMetaData metaData = connection.getMetaData();

            // 查询表的列信息
            ResultSet columns = metaData.getColumns(null, null, "tb_gerenshi", null);

            // 写入文件头
            fileWriter.write("字段名称,字段类型,备注信息\n");

            // 遍历结果集并写入文件
            while (columns.next()) {
                String columnName = columns.getString("COLUMN_NAME");
                String columnType = columns.getString("TYPE_NAME");
                String remarks = columns.getString("REMARKS");

                fileWriter.write(columnName + "," + columnType + "," + (remarks == null ? "" : remarks) + "\n");
            }

            System.out.println("表信息已成功写入到 " + outputFilePath);

        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }
}
